package com.turbo.boot.crawler.utils;

import java.util.Comparator;
import java.util.List;

/**
 * 描述
 *
 * @author wuyn
 * @since 2024/6/2
 */
public class PercentileUtils {

    public static double getPercentile(List<Double> dataList, double p) {
        int n = dataList.size();
        dataList.sort(new Comparator<Double>() {
            //从小到大排序
            @Override
            public int compare(Double o1, Double o2) {
                if (o1 == null || o2 == null) {
                    return 0;
                }
                return o1.compareTo(o2);
            }
        });
        double px = p * (n - 1);
        int i = (int) java.lang.Math.floor(px);
        double g = px - i;
        if (g == 0) {
            return dataList.get(i);
        } else {
            return (1 - g) * dataList.get(i) + g * dataList.get(i + 1);
        }
    }

}
